package com.ruoyi.web.controller.gcgl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcFkfp;
import com.ruoyi.gcgl.domain.GcProject;
import com.ruoyi.gcgl.domain.GcSupply;
import com.ruoyi.gcgl.domain.SjContract;
import com.ruoyi.gcgl.service.IGcFkfpService;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.gcgl.service.IGcSupplyService;
import com.ruoyi.gcgl.service.ISjContractService;

/**
 * 发票项Controller
 * 
 * @author ruoyi
 * @date 2023-07-21
 */
@Controller
@RequestMapping("/gcgl/fkfp")
public class GcFkfpController extends BaseController{
    private String prefix = "gcgl/fkfp";

    @Autowired
    private IGcFkfpService gcFkfpService;
    @Autowired
    private ISjContractService sjContractService;
    @Autowired
    private IGcSupplyService gcSupplyService;
    @Autowired
    private IGcProjectService gcProjectService;

    @RequiresPermissions("gcgl:fkfp:view")
    @GetMapping()
    public String fkfp()
    {
        return prefix + "/fkfp";
    }

    /**
     * 查询发票项列表
     */
    @RequiresPermissions("gcgl:fkfp:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcFkfp gcFkfp)
    {
        startPage();
        gcFkfp.setDeleteFlag(1);
        List<GcFkfp> list = gcFkfpService.selectGcFkfpList(gcFkfp);
        
        for(GcFkfp fkfp:list){
        	GcProject gcProject = gcProjectService.selectGcProjectById(fkfp.getGcProjectid());
        	fkfp.setStatus(gcProject.getStatus());
        }
        return getDataTable(list);
    }

    /**
     * 导出发票项列表
     */
    @RequiresPermissions("gcgl:fkfp:export")
    @Log(title = "发票项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcFkfp gcFkfp)
    {
    	gcFkfp.setDeleteFlag(1);
        List<GcFkfp> list = gcFkfpService.selectGcFkfpList(gcFkfp);
        ExcelUtil<GcFkfp> util = new ExcelUtil<GcFkfp>(GcFkfp.class);
        return util.exportExcel(list, "发票项数据");
    }

    /**
     * 新增发票项
	 * 1.供应商合同 
     */
    @GetMapping("/add/{id}/{type}")
    public String add(@PathVariable("id") Long id,@PathVariable("type") Long type, ModelMap mmap){
    	
//    	if(type==1l){
    		SjContract obj = sjContractService.selectSjContractById(id);
    		mmap.put("gcProjectid", obj.getGcProjectId());
        	mmap.put("projectId", id);
        	mmap.put("projectNo", obj.getOrderNo());
        	mmap.put("xmmc", obj.getProjectName());
        	mmap.put("xmtype", type);
        	mmap.put("supplyName1", obj.getCustomerName());
//    	}
        return prefix + "/add";
    }

    /**
     * 新增保存发票项
     */
    @RequiresPermissions("gcgl:fkfp:add")
    @Log(title = "发票项", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GcFkfp gcFkfp)
    {
    	try {
    		SimpleDateFormat sdy = new SimpleDateFormat("yyyy");
    		SimpleDateFormat sdm = new SimpleDateFormat("MM");
    		int year = Integer.parseInt(sdy.format(new Date()));
    		int month = Integer.parseInt(sdm.format(new Date()));
    		GcFkfp newObj = new GcFkfp();
    		newObj.setDeleteFlag(1);
    		newObj.setCreateAt(DateUtils.getFisrtDayOfMonth(year, 1));
    		newObj.setUpdateAt(DateUtils.getLastDayOfMonth(year, 12));
    		List<GcFkfp> lisCount = gcFkfpService.selectGcFkfpList(gcFkfp);
    		
    		String no = String.format("%02d", lisCount.size()+1);
    		gcFkfp.setOrderNo(year+"-"+no);
    		gcFkfp.setDeleteFlag(1);
    		gcFkfp.setCreateUser(getLoginName());
    		gcFkfp.setCreateAt(new Date());
    		gcFkfp.setUpdateUser(getLoginName());
    		gcFkfp.setUpdateAt(new Date());
    		gcFkfpService.insertGcFkfp(gcFkfp);
        	
		} catch (Exception e) {
			return error(e.getMessage());
		}
    	
        return toAjax(1);
    }

    /**
     * 修改发票项
     */
    @RequiresPermissions("gcgl:fkfp:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcFkfp gcFkfp = gcFkfpService.selectGcFkfpById(id);
        if(gcFkfp.getFap()!=null && !gcFkfp.getFap().equals("")){
        	String[] zlis = gcFkfp.getFap().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += zlis[i]+",";
        	}
        	mmap.put("fapvo", file);
        }else{
        	mmap.put("fapvo", "");
        }
        mmap.put("gcFkfp", gcFkfp);
        
        GcSupply gcSupply = new GcSupply();
        gcSupply.setDeleteFlag(1);
        gcSupply.setType(4);
        List<GcSupply> supplylis = gcSupplyService.selectGcSupplyList(gcSupply);
        mmap.put("supplylis", supplylis);
        return prefix + "/edit";
    }
    
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcFkfp gcFkfp = gcFkfpService.selectGcFkfpById(id);
        if(gcFkfp.getFap()!=null && !gcFkfp.getFap().equals("")){
        	String[] zlis = gcFkfp.getFap().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += RuoYiConfig.getOnPath()+zlis[i]+",";
        	}
        	mmap.put("fapvo", file);
        }else{
        	mmap.put("fapvo", "");
        }
        mmap.put("gcFkfp", gcFkfp);
        
        GcSupply gcSupply = new GcSupply();
        gcSupply.setDeleteFlag(1);
        gcSupply.setType(4);
        List<GcSupply> supplylis = gcSupplyService.selectGcSupplyList(gcSupply);
        mmap.put("supplylis", supplylis);
        return prefix + "/detail";
    }

    /**
     * 修改保存发票项
     */
    @RequiresPermissions("gcgl:fkfp:edit")
    @Log(title = "发票项", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcFkfp gcFkfp)
    {
    	try {
    		gcFkfp.setUpdateUser(getLoginName());
    		gcFkfp.setUpdateAt(new Date());
    		gcFkfpService.updateGcFkfp(gcFkfp);
		} catch (Exception e) {
			return error(e.getMessage());
		}
    	
        return toAjax(1);
    }

    /**
     * 删除发票项
     */
    @RequiresPermissions("gcgl:fkfp:remove")
    @Log(title = "发票项", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcFkfpService.deleteGcFkfpByIds(ids));
    }
    
    @GetMapping("/p4fkfp/{id}")
    public String p4fkfp(ModelMap mmap,@PathVariable("id") Long id){
    	GcProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
        return "sj/project/projectDetailFkfp";
    }

    /**
     * 查询其它付款申请单列表
     */
    @PostMapping("/p4list")
    @ResponseBody
    public TableDataInfo p4list(GcFkfp gcFkfp){
        startPage();
        gcFkfp.setDeleteFlag(1);
        List<GcFkfp> list = gcFkfpService.selectGcFkfpList(gcFkfp);
        
        for(GcFkfp fkfp:list){
        	GcProject gcProject = gcProjectService.selectGcProjectById(fkfp.getGcProjectid());
        	fkfp.setStatus(gcProject.getStatus());
        }
        return getDataTable(list);
    }
}